package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

import qlsl.androiddesign.activity.subactivity.AssessmentResultActivity;
import qlsl.androiddesign.entity.commonentity.FragmentArgs;
import qlsl.androiddesign.entity.otherentity.CarModel;
import qlsl.androiddesign.http.service.subservice.AssessmentService;
import qlsl.androiddesign.util.runfeng.DoubleRetain;
import qlsl.androiddesign.view.baseview.FunctionView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.qlsl.androiddesign.kuaiwa.R;

/**
 * 评估结果页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class AssessmentResultView extends
		FunctionView<AssessmentResultActivity> {

	private BarChart chart;

	private FragmentArgs args;

	private CarModel car;

	double AssessmentPrice;// 评估的价格

	public AssessmentResultView(AssessmentResultActivity activity) {
		super(activity);
		setContentView(R.layout.activity_assessment_result);
	}

	protected void initView(View view) {
		setTitle("评估结果");
		chart = findViewById(R.id.charts);
		TextView tv_brand = (TextView) findViewById(R.id.tv_brand);
		TextView tv_category = (TextView) findViewById(R.id.tv_category);
		TextView tv_date = (TextView) findViewById(R.id.tv_date);
		TextView tv_address = (TextView) findViewById(R.id.tv_address);
		TextView tv_time = (TextView) findViewById(R.id.tv_time);
		TextView is_dai = (TextView) findViewById(R.id.is_dai);
		SeekBar seek_bars = (SeekBar) findViewById(R.id.seek_bars);
		TextView color_name = (TextView) findViewById(R.id.color_name);

		ViewGroup parent = (ViewGroup) getContentView().getChildAt(0);
		TextView color_name2 = (TextView) parent.getChildAt(2).findViewById(
				R.id.color_name);
		seek_bars.setEnabled(false);
		color_name.setText("设备信息");
		color_name2.setText("评估价格");

		args = (FragmentArgs) activity.getIntent().getSerializableExtra("data");

		car = (CarModel) args.get("newprice");

		tv_brand.setText("" + args.get("tv_brands"));
		tv_category.setText("" + args.get("tv_categorys"));
		tv_date.setText("" + args.get("tv_dates"));
		tv_address.setText("" + args.get("tv_addresss"));
		tv_time.setText("" + args.get("forget_pwd_et_validate_codes"));
		if ((Integer) args.get("postion") == 1) {
			is_dai.setText("否");
		} else {
			is_dai.setText("是");
		}

		setBarChart();
	}

	protected void initData() {
		AssmentResult();
		AssessmentService.queryAssessment(this, activity);

	}

	private void AssmentResult() {
		TextView activity_new_price = (TextView) findViewById(R.id.activity_new_price);
		TextView activity_assment_center = (TextView) findViewById(R.id.activity_assment_center);
		TextView activity_assment_left = (TextView) findViewById(R.id.activity_assment_left);
		TextView activity_assment_right = (TextView) findViewById(R.id.activity_assment_right);
		RatingBar ratingBar = (RatingBar) findViewById(R.id.activity_rating);

		double newprice = car.getPrice();

		if ((Integer) args.get("postion") == 1) {
			AssessmentPrice = newprice
					- (Integer) args.get("useyear")
					* car.getYeardepreciate()
					- Integer.parseInt((String) args
							.get("forget_pwd_et_validate_codes"))
					* car.getHourdepreciate();

		} else {
			AssessmentPrice = newprice
					* 0.86
					- (Integer) args.get("useyear")
					* car.getYeardepreciate()
					- Integer.parseInt((String) args
							.get("forget_pwd_et_validate_codes"))
					* car.getHourdepreciate();

		}
		if (AssessmentPrice < 10000) {
//			int mas = (int) (Math.random() * 2 + 1);
			Random random=new Random();
			double mas = (random.nextInt(3)  + 1);
			activity_assment_center.setText(mas + "万");// 评估价格
			activity_assment_left.setText(mas - 0.5 + "万");// 本季度最低成交价格
			activity_assment_right.setText(mas + 2 + "万");// 本季度最高成交价格

			ratingBar.setRating((float) 1);// 保值率等级
		} else {
			activity_assment_center
					.setText(""
							+ DoubleRetain
									.doubleRetain4(AssessmentPrice / 10000)
							+ "万");// 评估价格
			activity_assment_left.setText(""
					+ DoubleRetain
							.doubleRetain4((AssessmentPrice - 15000) / 10000)
					+ "万");// 本季度最低成交价格
			if (AssessmentPrice + 20000 > newprice) {
				activity_assment_right.setText(newprice / 10000 + "万");// 本季度最高成交价格
			} else {
				activity_assment_right
						.setText(""
								+ DoubleRetain
										.doubleRetain4((AssessmentPrice + 20000) / 10000)
								+ "万");// 本季度最高成交价格
			}

			ratingBar.setRating((float) (AssessmentPrice / newprice) * 5);// 保值率等级
		}

		activity_new_price.setText("新车指导价:"
				+ DoubleRetain.doubleRetain4(newprice / 10000) + "万");
	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.assessment_again:
			activity.finish();
			break;

		default:
			break;
		}

	}

	public void setBarChart() {
		// apply styling
		chart.setDescription("");
		chart.setDrawGridBackground(false);
		chart.setDrawBarShadow(true);

		XAxis xAxis = chart.getXAxis();
		xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
		xAxis.setDrawGridLines(true);
		xAxis.setDrawAxisLine(true);

		chart.getAxisLeft().setEnabled(false);
		chart.getAxisRight().setEnabled(false);
		// YAxis leftAxis = chart.getAxisLeft();
		// leftAxis.setLabelCount(5, false);
		// leftAxis.setSpaceTop(5f);
		//
		// YAxis rightAxis = chart.getAxisRight();
		// rightAxis.setLabelCount(5, false);
		// rightAxis.setSpaceTop(5f);

		// set data
		chart.setData(generateDataBar());

		// do not forget to refresh the chart
		// holder.chart.invalidate();
		chart.animateY(700);

	}

	/**
	 * generates a random ChartData object with just one DataSet
	 * 
	 * @return
	 */
	private BarData generateDataBar() {

		ArrayList<BarEntry> entries = new ArrayList<BarEntry>();

		double newprice = car.getPrice();

		for (int i = 0; i < 6; i++) {
			int a = 1 + i;
			double aa = newprice - a * car.getYeardepreciate();
			// CarModel car = (CarModel) args.get("newprice");
			entries.add(new BarEntry((float) (aa), i));
		}

		BarDataSet d = new BarDataSet(entries, "价格");
		// 设置根据数据集合绘制柱图时候的一些属性
		d.setBarSpacePercent(10f);
		d.setColors(ColorTemplate.VORDIPLOM_COLORS);
		d.setHighLightAlpha(255);

		BarData cd = new BarData(getMonths(), d);
		return cd;
	}

	private ArrayList<String> getMonths() {

		ArrayList<String> m = new ArrayList<String>();
		Calendar c = Calendar.getInstance();
		Integer years = c.get(Calendar.YEAR);
		for (int i = 0; i < 6; i++) {
			m.add("" + (years + i));
		}
		return m;
	}

}
